package com.example.cr.insect.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.cr.common.response.PageResponse;
import com.example.cr.insect.entity.PreventionPlan;
import com.example.cr.insect.entity.PreventionPlanExample;
import com.example.cr.insect.mapper.PreventionPlanMapper;
import com.example.cr.insect.request.PreventionPlanListRequest;
import com.example.cr.insect.response.PreventionPlanResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
    import cn.hutool.core.date.DateTime;
    import cn.hutool.core.util.ObjectUtil;
    import com.example.cr.common.util.SnowflakeUtil;
    import com.example.cr.insect.request.PreventionPlanRequest;

@Service
public class PreventionPlanService {
private static final Logger log = LoggerFactory.getLogger(PreventionPlanService.class);
@Autowired
PreventionPlanMapper preventionPlanMapper;

public PageResponse<PreventionPlanResponse> list(PreventionPlanListRequest request) {
    PreventionPlanExample preventionPlanExample = new PreventionPlanExample();
    preventionPlanExample.setOrderByClause("id desc");

    // 处理搜索条件
    PreventionPlanExample.Criteria criteria = preventionPlanExample.createCriteria();
    if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
    String keyword = "%" + request.getKeyword() + "%";
                criteria.andNameLike(keyword);
    }

    PageHelper.startPage(request.getPage(), request.getSize());
    log.info("当前页码 current_page = {}", request.getPage());
    log.info("每页记录数 per_page = {}", request.getSize());

    List<PreventionPlan> preventionPlans = preventionPlanMapper.selectByExample(preventionPlanExample);

    PageInfo<PreventionPlan> pageInfo = new PageInfo<>(preventionPlans);
    log.info("总记录数 total = {}", pageInfo.getTotal());
    log.info("总页数 pages = {}", pageInfo.getPages());

    List<PreventionPlanResponse> list = BeanUtil.copyToList(preventionPlans, PreventionPlanResponse.class);

        PageResponse<PreventionPlanResponse> pageResponse = new PageResponse<>();
            pageResponse.setTotal(pageInfo.getTotal());
            pageResponse.setList(list);

            return pageResponse;
            }
            public void save(PreventionPlanRequest request) {
            PreventionPlan preventionPlan = BeanUtil.copyProperties(request, PreventionPlan.class);
            DateTime now = DateTime.now();
            if (ObjectUtil.isNull(preventionPlan.getId()) || preventionPlan.getId() == 0L) {
            preventionPlan.setId(SnowflakeUtil.getId());
            preventionPlan.setCreatedAt(now);
            preventionPlan.setUpdatedAt(now);
            preventionPlanMapper.insert(preventionPlan);
            } else {
            preventionPlan.setUpdatedAt(now);
            preventionPlanMapper.updateByPrimaryKeySelective(preventionPlan);
            }
            }

            public int deleteBatch(List<Long> ids) {
                PreventionPlanExample preventionPlanExample = new PreventionPlanExample();
                PreventionPlanExample.Criteria criteria = preventionPlanExample.createCriteria();
                criteria.andIdIn(ids);
                return preventionPlanMapper.deleteByExample(preventionPlanExample);
                }
                }
